# while循环
j = 1
result = 0
while j <= 100:
    result += j
    j += 1
print(result)

i = 1
result = 0
while i <= 100:
    if i % 2 == 0:
        result += i
    i += 1
print(result)

i = 0
result = 0
while i <= 100:
    result += i
    i += 2
print(result)

# break、continue
# break:跳出循环
i = 1
while i <= 5:
    if i == 4:
        print("不吃了")
        break
    print(f"吃的第{i}个苹果")
    i += 1
# continue:跳出本轮循环
i = 1
while i <= 5:
    if i == 3:
        print(f"大虫子，第{i}个不吃了")
        i += 1
        # continue前必须更改i值 否则会陷入死循环
        continue
    print(f"吃的第{i}个苹果")
    i += 1

# while的嵌套循环
j = 0
while j <= 4:
    i = 0
    while i <= 4:
        # 一行内的星星不能换行，取消print默认结束符\n
        print('*', end=' ')
        i += 1
    # 每行结束要换行，这里是借助一个空的print，利用print默认结束符换行
    print()
    j += 1

j = 0
while j <= 4:
    i = 0
    # i表示每行里星星的个数，这个数字要和行号相等，所以i要和j联动
    while i <= j:
        # 一行内的星星不能换行，取消print默认结束符\n
        print('*', end=' ')
        i += 1
    # 每行结束要换行，这里是借助一个空的print，利用print默认结束符换行
    print()
    j += 1

j = 1
while j <= 9:
    i = 1
    while i <= j:
        print(f'{i}*{j}={j*i}', end='\t')
        # \t 制表
        i += 1
    # 每行结束要换行，这里是借助一个空的print，利用print默认结束符换行
    print()
    j += 1

# for循环
# break:
str1 = 'itheima'
for i in str1:
    if i == 'e':
        print("遇到e不打印")
        break
    print(i)

# continue:
str1 = 'itheima'
for i in str1:
    if i == 'e':
        print("遇到e不打印")
        continue
    print(i)

# while else, for else 循环正常结束后执行的代码
str1 = 'itheima'
for i in str1:
    if i == 'e':
        print("遇到e不打印")
        break
    print(i)
else:
    print("循环正常结束后执行的代码")

str1 = 'itheima'
for i in str1:
    if i == 'e':
        print("遇到e不打印")
        continue
    print(i)
else:
    print("循环正常结束后执行的代码")
# continue是退出当前一次循环，继续下一次循环，所以该循环在continue控制下是可以正常结束的。当循环结束后执行else缩进的代码。

